package top.greatxiaozou.config;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import top.greatxiaozou.pojo.Admin;
import top.greatxiaozou.pojo.User;
import top.greatxiaozou.service.AdminService;
import top.greatxiaozou.service.UserService;

import java.util.HashSet;
import java.util.Set;


public class UserRealm extends AuthorizingRealm {
    @Autowired
    private UserService userService;
    @Autowired
    private AdminService adminService;

    //用户授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //Subject subject = SecurityUtils.getSubject();

        return info;
    }


    //用户认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        User user = userService.selectUserByName(token.getUsername());
       // System.out.println(token.getUsername());
        if (user==null){
            return null;            //此处抛出异常
        }else {
            return new SimpleAuthenticationInfo(user,user.getPassword(),"");
        }



    }
}
